import store from '../store'
import Config from '../common/js/config'

export default {
	data() {
		return {}
	},
	onShow() {
		if (process.env.NODE_ENV === 'development') {
			const page = getCurrentPages()
			if (page && page.length > 0) {
				console.log(page[page.length - 1].route);
			}
		}
	},
	computed: {
		gUserInfo: {
			get() {
				return store.state.userInfo
			},
			set(val) {
				console.error('不可以直接修改gUserInfo属性，请通过Vuex action事件"updateUserInfo"更新')
			}
		},
		gShopInfo: {
			get() {
				return store.state.shopInfo
			},
			set(val) {
				console.error('不可以直接修改gShopInfo属性，请通过Vuex action事件"updateShopInfo"更新')
			}
		},
		gSalesTicketGoodsList: {
			get() {
				return store.state.salesTicketGoodsList
			},
			set(val) {
				console.error('不可以直接修改gShopInfo属性，请通过Vuex action事件"updateShopInfo"更新')
			}
		}
	},
	methods: {
		setData: function(obj, callback) {
			let that = this;
			let keys = [];
			let val, data;
			Object.keys(obj).forEach(function(key) {
				keys = key.split('.');
				val = obj[key];
				data = that.$data;
				keys.forEach(function(key2, index) {
					if (index + 1 == keys.length) {
						that.$set(data, key2, val);
					} else {
						if (!data[key2]) {
							that.$set(data, key2, {});
						}
					}
					data = data[key2];
				})
			});
			callback && callback();
		},
		concatRequestUrl(data) {
			if (typeof data === 'string' && data.indexOf('http://') == -1 && data.indexOf('https://') == -1) {
				return `${Config.requestUrl}${data}`
			}
			return data
		}
	}
}
